#include "FileSystemModal.h"

#include "Config.h"
#include <QDate>
#include <QFileIconProvider>
#include <algorithm>
FileSystemModal::FileSystemModal(void)
{
	deep_ = false;
}


FileSystemModal::~FileSystemModal(void)
{
}
QVariant FileSystemModal::data(const QModelIndex & index, int role) const
{
	if ( role == Qt::DisplayRole)
	{
		switch (index.column())
		{
		case TableConfig::CN_FILE_NAME:
			return QVariant(data_.at(index.row()).baseName());
			break;
		case TableConfig::CN_LAST_MODIFY_TIME:
			return QVariant(data_.at(index.row()).lastModified().toString(TableConfig::TIME_FORMAT));
			break;
		case TableConfig::CN_CREATE_TIME:
			return QVariant(data_.at(index.row()).created().toString(TableConfig::TIME_FORMAT));
			break;
		case TableConfig::CN_AB_PATH:
			return QVariant(data_.at(index.row()).absoluteFilePath());
			break;
		case TableConfig::CN_EXT:
			return QVariant(data_.at(index.row()).suffix());
			break;
		default:
			return QVariant();
		}
	}
	if (role==Qt::DecorationRole)
	{
		switch (index.column())
		{
		case TableConfig::CN_FILE_NAME:
			return QVariant(QFileIconProvider().icon(data_.at(index.row())));
			break;
		}
	}
	return QVariant();
	
}

void FileSystemModal::setCurrentPath(const QString& path)
{
	cur_path_ = path;
	fillData();
	setRowCount(data_.size());
}
QString FileSystemModal::getCurrentPath()
{
	return cur_path_;
}
void FileSystemModal::fillData()
{
	if (!data_.empty())
	{
		data_.clear();
	}
	travelFolder(cur_path_,data_,deep_);

}
void FileSystemModal::travelFolder(const QString& dir,std::vector<QFileInfo>& fileInfos,bool bDeep)
{
	QDir rootDir(dir);

	foreach(QFileInfo fileInfo,rootDir.entryInfoList())
	{
		if (fileInfo.isDir())
		{
			if (bDeep)
			{
				travelFolder(fileInfo.absoluteFilePath(),fileInfos,bDeep);
			}

		}
		else
		{
			fileInfos.push_back(fileInfo);
		}
	}
}
void FileSystemModal::setIsTravelDeep(bool isdeep)
{
	deep_ = isdeep;

}
bool FileSystemModal::isTravelDeep()
{
	return deep_;
}

bool  FileSystemModal::removeRows(int row, int count, const QModelIndex &parent /* = QModelIndex */)
{
	data_.erase(data_.begin()+row,data_.begin()+row+count);
	setRowCount(data_.size());
	return true;
}